package com.sfang.common.util.decimal;

import java.math.BigDecimal;

public class BigDecimalUtil {

    public static BigDecimal integerVal(BigDecimal bigDecimal) {
        return bigDecimal.setScale(0, BigDecimal.ROUND_DOWN);
    }

    public static BigDecimal percent(Integer numerator, Integer denominator) {
        try {
            return new BigDecimal(numerator).divide(new BigDecimal(denominator), 2, BigDecimal.ROUND_UP).multiply(new BigDecimal(100)).setScale(0);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal divideWithScale(Integer numerator, Integer denominator) {
        try {
            return new BigDecimal(numerator).divide(new BigDecimal(denominator), 4, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal divide(Integer numerator, Integer denominator) {
        try {
            return new BigDecimal(numerator).divide(new BigDecimal(denominator), 4, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal percentWithScale(Integer numerator, Integer denominator) {
        try {
            return new BigDecimal(numerator).divide(new BigDecimal(denominator), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal percentWithScale(BigDecimal numerator, BigDecimal denominator) {
        try {
            return numerator.divide(denominator, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal percentWithScale(Long numerator, Long denominator) {
        try {
            return new BigDecimal(numerator).divide(new BigDecimal(denominator), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal percent(BigDecimal numerator, BigDecimal denominator) {
        try {
            return numerator.divide(denominator, 2, BigDecimal.ROUND_UP).setScale(0);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal divideWithScale(BigDecimal numerator, BigDecimal denominator) {
        try {
            return numerator.divide(denominator, 2, BigDecimal.ROUND_HALF_UP).setScale(2);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal multiply(Integer numerator, Integer denominator) {
        try {
            return new BigDecimal(numerator).multiply(new BigDecimal(denominator)).setScale(0, BigDecimal.ROUND_DOWN);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static BigDecimal multiplyRoundUp(Integer numerator, Double denominator) {
        try {
            return new BigDecimal(numerator).multiply(new BigDecimal(denominator)).setScale(0, BigDecimal.ROUND_UP);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public static Integer multiply(Integer numerator, BigDecimal denominator) {
        try {
            return new BigDecimal(numerator).multiply(denominator).setScale(0, BigDecimal.ROUND_DOWN).intValue();
        } catch (Exception e) {
            return -1;
        }
    }

    public static BigDecimal multiply(int numerator, double value) {
        try {
            return BigDecimal.valueOf(numerator).multiply(BigDecimal.valueOf(value)).setScale(0, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }
}
